package com.acme.bankapp.service.bank;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.acme.bankapp.domain.bank.Account;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;

public class BankReport {
	/**
	 * Method return number of customers
	 * @param bank
	 * @return
	 */
	public static long getNbOfCustomers(Bank bank) {
		return bank.getClients().size();
	}
	
	/**
	 * Method accumulate number of all accounts
	 * @param bank
	 * @return
	 */
	public static long getNbOfAccounts(Bank bank) {
		long count = 0;
		for (Client cli: bank.getClients()) {
			count += cli.getAccounts().size();
		}
		return count;
	}
	
	/** 
	 * Return Sorted List of Accounts 
	 * @param bank
	 * @return
	 */
	public static List<Account> getAccountsSortedBySum(Bank bank) {
		List<Account> accounts = new LinkedList<Account>();
		for (Client cli: bank.getClients()) {
			accounts.addAll(cli.getAccounts());
		}
		Collections.sort(accounts);
		return accounts; 
	}
	
	/**
	 * Accumulate all negative balance, which indicate on credit
	 * @param bank
	 * @return
	 */
	public static double getBankCredit(Bank bank) {
		double count = 0;
		for (Client cli: bank.getClients()) {
			for (Account acc: cli.getAccounts()) {
				if (acc.getBalance() < 0)
				count += (-acc.getBalance());
			}
		}
		return count;
	}
	
	/**
	 * Method return map with key Client, value Account
	 * Map is backed on HashMap for Client quick search 
	 * @param bank
	 * @return
	 */
	public static Map<Client, Account> getCustomerAccount(Bank bank) {
		Map<Client, Account> map = new HashMap<>();
		for (Client cli: bank.getClients()) {
			for (Account acc: cli.getAccounts()) {
				map.put(cli, acc);
			}
		}
		return map;
	}
	
}
